package com.epoint.mobileoa.personalized.epointcompany.ui;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Intent;
import android.text.Html;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.personalized.epointcompany.bizlogic.PersonalRizhiListTask;
import com.epoint.mobileoa.personalized.epointcompany.model.PersonalRiziListModel;
import com.epoint.mobileoa.service.MainService;
import com.epoint.mobileoa.tools.DateHelp;
import com.epoint.mobileoa.tools.StringHelp;

/**
 * @author liyc
 * @time 2012-3-13 下午2:24:24
 * @annotation 个人日志列表视图
 */
public class PersonalRiziListView extends SuperView {

	private ListView lv;
	private List<PersonalRiziListModel> list = new ArrayList<PersonalRiziListModel>();

	private Button btDtrztx;//加载中的文本
	private LinearLayout foot;

	int lastYear = 0;
	int lastMonth = 0;
	private String serverDate;
	private RZListAdapter adapter = new RZListAdapter();
	
	@Override
	public void initControls() {
		btDtrztx = (Button) findViewById(R.id.btDtrztx);
		btDtrztx.setOnClickListener(this);
		top_right_bt.setVisibility(View.VISIBLE);
		top_right_bt.setOnClickListener(this);
		top_right_bt.setText("填写");
		lv = (ListView) findViewById(R.id.epoint_personalrizhilist_lv);
		lv.setOnItemClickListener(this);
		addFoot();
		lv.setAdapter(adapter);
	}

	private void getData(int year,int month){
		showDialog_wait(null);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("year", year);
		map.put("month", month);
		Task task = new PersonalRizhiListTask(this, map);
		MainService.addTask(task);
	}

	public void addFoot(){
		TextView load_bt = new TextView(this);
		load_bt.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
		load_bt.setText("更多...");
		load_bt.setGravity(Gravity.CENTER);
		foot = new LinearLayout(this);
		foot.addView(load_bt, new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT));
		foot.setGravity(Gravity.CENTER);
		lv.addFooterView(foot);
		registerForContextMenu(lv);
	}

	@Override
	public void onClick(View v) {
		if(v==top_right_bt){
			for(PersonalRiziListModel p:list){
				if(p.rzdate.contains(serverDate)){
					Intent2Detail(p);
				}
			}
		}
		else if(v==btDtrztx){
			for(PersonalRiziListModel p:list){
				if(p.rzdate.contains(serverDate)){
					Intent2Detail(p);
				}
			}
		}
	}

	private void Intent2Detail(PersonalRiziListModel p){
		Intent it = new Intent(this,RZTianxieView.class);
		it.putExtra("cczt", p.cczt);
		it.putExtra("shzt", p.shzt);
		it.putExtra("rzdate", p.rzdate);
		it.putExtra("ldps", p.ldps);
		it.putExtra("psryname", p.psryname);
		it.putExtra("gzrzguid", p.gzrzguid);
		it.putExtra("psdate",  p.psdate);
		it.putExtra("ccguid",  p.ccguid);
		it.putExtra("qjguid",  p.qjguid);
		it.putExtra("lyguid",  p.lyguid);
		it.putExtra("chuchaisj",  p.chuchaisj);
		it.putExtra("serverDateInt", serverDateInt);
		it.putExtra("clickdate", clickdate);
		startActivity(it);
	}


	@Override
	public void refresh(Object... param) {
		if(param[0].toString().equals(MainService.NORMAL)){
			List<PersonalRiziListModel> onelist = PersonalRizhiListTask.XmlAnalysis(StringHelp.getAttOut(param[1].toString(), "rizhilists"));

			for(PersonalRiziListModel prm:onelist){
				list.add(prm);
			}
			serverDate = StringHelp.getXMLAtt(param[1].toString(), "serverDate");
			
			adapter.notifyDataSetChanged();

			/*if(lastYear==0&&lastMonth==0){
				
				for(int i=0;i<list.size();i++){
					if (list.get(i).rzdate.contains(serverDate)) {
						lv.setSelection(i);
					}
				}
			}*/
			
			lastYear = Integer.parseInt(StringHelp.getXMLAtt(param[1].toString(), "lastyear"));
			lastMonth = Integer.parseInt(StringHelp.getXMLAtt(param[1].toString(), "lastmonth"));
			
//			int thisYear = Integer.parseInt(StringHelp.getXMLAtt(param[1].toString(), "thisyear"));
//			int thisMonth = Integer.parseInt(StringHelp.getXMLAtt(param[1].toString(), "thismonth"));
		}
		m_Dialog.dismiss();
	}

	@Override
	public int getLayoutInt() {
		return R.layout.personalrizhilistview;
	}

	@Override
	public String setTitle() {
		return "我的日志";
	}

	Integer clickdate;
	Integer serverDateInt;
	@Override
	public void onItemClick(AdapterView<?> parent, View view,
			int position, long id) {
		if(position==list.size()){
			getData(lastYear,lastMonth);
			return;
		}
		clickdate = Integer.parseInt(list.get(position).rzdate.substring(0, 10).replaceAll("-", ""));
		serverDateInt = Integer.parseInt(serverDate.replaceAll("-", ""));
		if(clickdate<=serverDateInt){
			Intent2Detail(list.get(position));
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		list = new ArrayList<PersonalRiziListModel>();
		lastYear = 0;
		lastMonth = 0;
		getData(lastYear,lastMonth);
	}
	
	class RZListAdapter extends BaseAdapter
	{

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater mInflater = LayoutInflater.from(PersonalRiziListView.this);
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.personrizhilistlayout, null);
				holder.ml_mailimg = (ImageView) convertView
						.findViewById(R.id.noticelist_img);
				holder.ml_attach = (ImageView) convertView
						.findViewById(R.id.noticelist_attach);
				holder.ml_mailtitle = (TextView) convertView
						.findViewById(R.id.noticelist_title);
				holder.ml_sandt = (TextView) convertView
						.findViewById(R.id.noticelist_send);
				holder.notice_time = (TextView) convertView
						.findViewById(R.id.noticelist_time);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.ml_mailimg.setVisibility(View.GONE);
			holder.ml_attach.setVisibility(View.GONE);

			String rzdate = list.get(position).rzdate;

			Date dateLong = DateHelp.convertString2Date(rzdate, "yyyy-MM-dd hh:mm:ss");

			String dateShort = list.get(position).rzdate.substring(0, 10);
			String week = DateHelp.getWeekByDate(dateLong);
			
			String titleColor = "";
			if(PersonalRiziListView.this.getBackgroundStr().equals("background")){
				titleColor = "white";
			}
			else{
				titleColor = "black";
			}

			if(week.equals("星期日")){
				week = "<font color=#FF2D2D>(星期日)</font>";
			}
			else{
				week = "<font color='"+titleColor+"'>("+week+")</font>";
			}
			
			if(serverDate.equals(dateShort)){
				holder.ml_attach.setVisibility(View.VISIBLE);
				holder.ml_attach.setBackgroundResource(R.drawable.img_today_flag2);
			}
			
			
			dateShort = "<font color='"+titleColor+"'>"+dateShort+"</font>";
			
			holder.ml_mailtitle.setText(Html.fromHtml(dateShort+week));

			holder.ml_sandt.setText(Html.fromHtml("<font color=red>"+list.get(position).shzt+"</font>"));

			if(list.get(position).cczt.equals("出差")){
				holder.notice_time.setText("出差");
			}
			else if(list.get(position).cczt.equals("旅游")){
				holder.notice_time.setText(Html.fromHtml("<font color=#82D900>旅游</font>"));
			}
			else if(list.get(position).cczt.equals("请假")){
				holder.notice_time.setText(Html.fromHtml("<font color=#AFAF61>请假</font>"));
			}
			else {
				holder.notice_time.setText("");
			}

			return convertView;
		}
		
	}
	
	public class ViewHolder {
		public ImageView ml_mailimg;
		public ImageView ml_attach;
		public TextView ml_mailtitle;
		public TextView ml_sandt;
		public TextView notice_time;
	}

	@Override
	public void refreshForNewTask(int TaskId,Object... param) {
	}
}
